package com.bjx.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjx.api.system.domain.vo.DictDataVO;
import com.bjx.common.core.constant.RedisConst;
import com.bjx.common.redis.service.RedisService;
import com.bjx.system.domain.entity.DictDataEntity;
import com.bjx.system.mapper.DictDataMapper;
import com.bjx.system.service.IDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @desc: 字典数据业务实现
 * @author: liushuai
 * @date: 2022/8/16
 */
@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictDataEntity> implements IDictDataService {
    @Autowired
    private DictDataMapper dictDataMapper;
    @Autowired
    private RedisService redisService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveDictData(List<DictDataVO> vos, String dictTypeCode) {
        LambdaQueryWrapper<DictDataEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictDataEntity::getDictTypeCode,dictTypeCode);
        dictDataMapper.delete(wrapper);
        if(CollectionUtil.isNotEmpty(vos)){
           List<DictDataEntity> list = new ArrayList<DictDataEntity>();
           vos.forEach(res ->{
               DictDataEntity entity = Convert.convert(DictDataEntity.class,res);
               entity.setDictTypeCode(dictTypeCode);
               entity.setDictDataId(null);
               list.add(entity);
           });
           this.saveBatch(list);
        }
    }


    @Override
    public List<DictDataVO> listDictDataByDictTypeCode(String dictTypeCode) {
        LambdaQueryWrapper<DictDataEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictDataEntity::getDictTypeCode,dictTypeCode);
        List<DictDataEntity> dictDataEntities = dictDataMapper.selectList(wrapper);
        List<DictDataVO> dictDataVOS = Convert.toList(DictDataVO.class, dictDataEntities);
        return dictDataVOS;
    }

    @Override
    public void deleteDictDataByDictTypeCode(String dictTypeCode) {
        LambdaQueryWrapper<DictDataEntity> wrapper = new LambdaQueryWrapper();
        wrapper.eq(DictDataEntity::getDictTypeCode,dictTypeCode);
        dictDataMapper.delete(wrapper);
    }

    @Override
    public List<DictDataVO> listCacheDictDataByDictTypeCode(String dictTypeCode) {
        List<DictDataVO> dictList = redisService.getCacheObjectList(RedisConst.SYSTEM_DICT_DATA + dictTypeCode,DictDataVO.class);
        if(CollectionUtil.isNotEmpty(dictList)){
            return dictList;
        }
        dictList = listDictDataByDictTypeCode(dictTypeCode);
        redisService.setCacheObject(RedisConst.SYSTEM_DICT_DATA + dictTypeCode,dictList);
        return dictList;
    }

    @Override
    public void clearCacheByDictTypeCode(String dictTypeCode) {
        redisService.deleteObject(RedisConst.SYSTEM_DICT_DATA + dictTypeCode);
    }


}
